package com.jihox.abookpro.dao;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Vector;

import com.jihox.abookpro.domain.Background;
import com.jihox.abookpro.util.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BackgroundDAO {
	
	private Context context;
	private DBHelper dbhelper;
	private SQLiteDatabase sqlitedatabase;

	String backgroundDir = null;
	String innerPageBackgroundThumbnailsDir = null;
	String coverPageBackgroundThumbnailsDir = null;
	
	public BackgroundDAO(Context context) {
		super();
		this.context = context;
	}
	
	public void opendb() {
		dbhelper = new DBHelper(context);
		sqlitedatabase = dbhelper.getWritableDatabase();
	}

	public void closedb() {
		if (sqlitedatabase != null && sqlitedatabase.isOpen()) {
			sqlitedatabase.close();
		}
	}
	
	//向数据库中添加一条Background
	public void addBackground(Background background) {
		ContentValues cv = new ContentValues();
		cv.put("Name", background.getName());
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
		background.getPicture().compress(Bitmap.CompressFormat.JPEG, 100, os);
		cv.put("Picture", os.toByteArray());
		opendb();
		sqlitedatabase.insert("background", null, cv);
		closedb();
	}
	
	//从数据库中删除一条Background
	public void deleteBackground(String name) {
		opendb();
		try {
			sqlitedatabase.delete("background", "Name = ?", new String[] {name});
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closedb();
		}
	}
	
	//根据name查询特定的Background
	public Background getBackground(String name) {
		Background background = new Background();
		opendb();
		Cursor c = sqlitedatabase.rawQuery("SELECT * FROM background where Name = '" + name +"'", null);
		while(c.moveToNext()){
			background.setName(c.getString(c.getColumnIndex("Name")));
			byte[] in=c.getBlob(c.getColumnIndex("Picture"));  
			background.setPicture(BitmapFactory.decodeByteArray(in,0,in.length));  
		}
		c.close();
		closedb();
		return background;
	}

	
	public BackgroundDAO(String backgroundDir){
		
		this.backgroundDir = backgroundDir;
		innerPageBackgroundThumbnailsDir = backgroundDir + File.separator + "Thumbnails" + File.separator + "InnerPage";
		coverPageBackgroundThumbnailsDir = backgroundDir + File.separator + "Thumbnails" + File.separator + "CoverPage";
	}
	
	public Vector<String> retrieveInnerPageBackgroundThumbnails(){
		Vector<String> thumbnails = new Vector<String>();
		String[] files =  new File(innerPageBackgroundThumbnailsDir).list();
		for (String file:files){
			thumbnails.add(innerPageBackgroundThumbnailsDir + File.separator + file);
		}
		return thumbnails;
	}
	
	public Vector<String> retrieveCoverPageBackgroundThumbnails(){
		Vector<String> thumbnails = new Vector<String>();
		String[] files =  new File(coverPageBackgroundThumbnailsDir).list();
		for (String file:files){
			thumbnails.add(coverPageBackgroundThumbnailsDir + File.separator + file);
		}
		return thumbnails;
	}
	
	public String retrieveBackgroundByThumbnails(String thumbnail){
		String fileName =  new File(thumbnail).getName();
		
		return backgroundDir +File.separator +fileName;
	}
}
